/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "KeyConverter.h"
#include "sdl.h"
#include <algorithm>
#include <cctype>
#include <iostream>

KeyConverter::KeyConverter(void)
{
}

// Converts the given key to a string (Ex. SDLK_BACKSPACE would equal "backspace")
// NOTE: look into SDL_GetKeyName() to help you out here.
std::string KeyConverter::ToString( int key ) 
{ 
    std::string k;
    switch(key)
    {
    case SDLK_BACKSPACE:
        k="BACKSPACE";
        break;
	case SDLK_TAB:
        k="TAB";
        break;
	case SDLK_RETURN:
        k="RETURN";
        break;
	case SDLK_PAUSE:
        k="PAUSE";
        break;
	case SDLK_ESCAPE:
        k="ESC";
        break;
	case SDLK_SPACE:
        k="SPACE";
        break;
	case SDLK_EXCLAIM:
        k="EXCLAIMATION";
        break;
	case SDLK_QUOTEDBL:
        k="DOUBLEQUOTE";
        break;
	case SDLK_HASH:
        k="POUNDSIGN";
        break;
	case SDLK_DOLLAR:
        k="DOLLAR";
        break;
	case SDLK_AMPERSAND:
        k="AMPERSAND";
        break;
	case SDLK_QUOTE:
        k="QUOTE";
        break;
	case SDLK_LEFTPAREN:
        k="LEFTPAREN";
        break;
	case SDLK_RIGHTPAREN:
        k="RIGHTPAREN";
        break;
	case SDLK_ASTERISK:
        k="ASTERISK";
        break;
	case SDLK_PLUS:
        k="PLUS";
        break;
	case SDLK_COMMA:
        k="COMMA";
        break;
	case SDLK_MINUS:
        k="MINUS";
        break;
	case SDLK_PERIOD:
        k="PERIOD";
        break;
	case SDLK_SLASH:
        k="SLASH";
        break;
    case SDLK_COLON:
        k="COLON";
        break;
	case SDLK_SEMICOLON:
        break;
	case SDLK_LESS:
        break;
	case SDLK_EQUALS:
        break;
	case SDLK_GREATER:
        break;
	case SDLK_QUESTION:
        break;
	case SDLK_AT:
        break;
    case SDLK_LEFTBRACKET:
        break;
	case SDLK_BACKSLASH:
        break;
	case SDLK_RIGHTBRACKET:
        break;
	case SDLK_CARET:
        break;
	case SDLK_UNDERSCORE:
        break;
	case SDLK_BACKQUOTE:
        break;
    case SDLK_DELETE:
        break;
    case SDLK_UP:
        k="UP";
        break;
	case SDLK_DOWN:
        k="DOWN";
        break;
	case SDLK_RIGHT:
        k="RIGHT";
        break;
	case SDLK_LEFT:
        k="LEFT";
        break;
	case SDLK_INSERT:
        break;
	case SDLK_HOME:
        break;
	case SDLK_END:
        break;
	case SDLK_PAGEUP:
        break;
	case SDLK_PAGEDOWN:
        break;
    case SDLK_F1:
        break;
	case SDLK_F2:
        break;
	case SDLK_F3:
        break;			
	case SDLK_F4:
        break;			
	case SDLK_F5:
        break;			
	case SDLK_F6:
        break;			
	case SDLK_F7:
        break;			
	case SDLK_F8:
        break;			
	case SDLK_F9:
        break;			
	case SDLK_F10:
        break;		
	case SDLK_F11:
        break;		
	case SDLK_F12:
        break;		
	case SDLK_F13:
        break;		
	case SDLK_F14:
        break;		
	case SDLK_F15:
        break;		
    case SDLK_NUMLOCK:
        break;		
	case SDLK_CAPSLOCK:
        break;		
	case SDLK_SCROLLOCK:
        break;		
	case SDLK_RSHIFT:
        k="RSHIFT";
        break;		
	case SDLK_LSHIFT:
        k="LSHIFT";
        break;		
	case SDLK_RCTRL:
        k="RCTRL";
        break;
	case SDLK_LCTRL:
        k="LCTRL";
        break;		
	case SDLK_RALT:
        k="RALT";
        break;		
	case SDLK_LALT:
        k="LALT";
        break;		
	case SDLK_RMETA:
        break;		
	case SDLK_LMETA:
        break;		
	case SDLK_LSUPER:
        break;		/* Left "Windows" key */
	case SDLK_RSUPER:
        break;				/* Right "Windows" key */
	case SDLK_MODE:
        break;		
    case SDLK_HELP:
        break;		
	case SDLK_PRINT:
        break;		
	case SDLK_SYSREQ:
        break;		
	case SDLK_BREAK:
        break;	
	case SDLK_MENU:
        break;		
	case SDLK_POWER:
        break;				/* Power Macintosh power key */
	case SDLK_UNDO:
        break;
    default:
        k = SDL_GetKeyName( static_cast<SDLKey>(key) );
        break;
    };
    return k; 
}

// Converts the given string into a valid key code (Ex. "backspace" would equal 8)
int KeyConverter::ToKeyCode( std::string &key ) 
{ 
    //std::string key;
    std::transform( key.begin(), key.end(), key.begin(), std::toupper );
    int k=0;
    if ( key == "" )
        return k;

    if ( key == "UP" ) {
        k = SDLK_UP;
    } else if ( key == "DOWN" ) {
        k = SDLK_DOWN;
    } else if ( key == "LEFT" ) {
        k = SDLK_LEFT;
    } else if ( key == "RIGHT" ) {
        k = SDLK_RIGHT;
    } else if ( key == "LCTRL" ) {
        k = SDLK_LCTRL;
    } else if ( key == "RCTRL" ) {
        k = SDLK_RCTRL;
    } else if ( key == "LALT" ) {
        k = SDLK_LALT;
    } else if ( key == "RALT" ) {
        k = SDLK_RALT;
    } else if ( key == "LSHIFT" ) {
        k = SDLK_LSHIFT;
    } else if ( key == "RSHIFT" ) {
        k = SDLK_RSHIFT;
    } else if ( key == "BACKSPACE" ) {
        k = SDLK_BACKSPACE;
    } else if ( key == "TAB" ) {
        k = SDLK_TAB;
    } else if ( key == "RETURN" ) {
        k = SDLK_RETURN;
    } else if ( key == "ESC" ) {
        k = SDLK_ESCAPE;
    } else if ( key == "SPACE" ) {
        k = SDLK_SPACE;
    } else {
        char c = key[0] + 32;
        k = c;
    }
    return k; 
}

// Returns the shift value of the key (Ex. SDLK_A would return 'A')
char KeyConverter::GetShiftValue( int key ) 
{ 
    int k;
    switch(key)
    {
	case SDLK_COMMA:
        k= SDLK_LESS;
        break;
	case SDLK_MINUS:
        k=SDLK_UNDERSCORE;
        break;
	case SDLK_PERIOD:
        k= SDLK_GREATER;
        break;
	case SDLK_SLASH:
        k= SDLK_QUESTION;
        break;
	case SDLK_SEMICOLON:
        k = SDLK_COLON;
        break;
	case SDLK_EQUALS:
        k = SDLK_PLUS;
        break;
    case SDLK_LEFTBRACKET:
        k = 123; // ascii value
        break;
	case SDLK_BACKSLASH:
        k = 124;
        break;
	case SDLK_RIGHTBRACKET:
        k = 125;
        break;
	case SDLK_BACKQUOTE:
        k = 126;
        break;	
    case SDLK_0:
        k = SDLK_RIGHTPAREN;
        break;
    case SDLK_1:
        k = SDLK_EXCLAIM;
        break;
    case SDLK_2:
        k = SDLK_AT;
    break;
    case SDLK_3:
        k = SDLK_HASH;
        break;
    case SDLK_4:
        k = SDLK_DOLLAR;
        break;
    case SDLK_5:
        k = 37;
        break;
    case SDLK_6:
        k = SDLK_CARET;
        break;
    case SDLK_7:
        k = SDLK_AMPERSAND;
        break;
    case SDLK_8:
        k = SDLK_ASTERISK;
        break;
    case SDLK_QUOTE:
        k = SDLK_QUOTEDBL;
        break;
    case SDLK_9:
        k = SDLK_LEFTPAREN;
        break;
    default:
        k = key - 32;
        break;
    };
    return k; 
}

KeyConverter::~KeyConverter(void)
{
}
